{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Python基础教程 Json简介"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "<font color=purple size=5>JSON（JavaScript Object Notation）是一种轻量级的数据交换格式，易于人阅读和编写</font>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "json.dumps 用于将Python对象编码成JSON字符串"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"b\": 2, \"d\": 4, \"a\": 1, \"c\": 3, \"e\": 5}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "data=[{'b':2,'d':4,'a':1,'c':3,'e':5}]\n",
    "json=json.dumps(data)\n",
    "print(json)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "为了提高可读性，dumps方法提供了一些可选的参数。\n",
    "\n",
    "sort_keys=True表示按照字典排序(a到z)输出。\n",
    "\n",
    "indent参数，代表缩进的位数\n",
    "\n",
    "separators参数的作用是去掉,和:后面的空格，传输过程中数据越精简越好"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    {\n",
      "        \"a\":1,\n",
      "        \"b\":2,\n",
      "        \"c\":3,\n",
      "        \"d\":4,\n",
      "        \"e\":5\n",
      "    }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "data = [ { 'b' : 2, 'd' : 4, 'a' : 1, 'c' : 3, 'e' : 5 } ]\n",
    "json = json.dumps(data, sort_keys=True, indent=4,separators=(',', ':'))\n",
    "print(json)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "jsonData = '{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}'\n",
    "text = json.loads(jsonData)  #将string转换为dict\n",
    "print(text)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}